package com.invengo.module_check.ui.fragment.first_check.widget;

import android.view.LayoutInflater;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProviders;

import com.afollestad.materialdialogs.MaterialDialog;
import com.invengo.device.DeviceHelper;
import com.invengo.library_db.entity.module_check.ShelfBean;
import com.invengo.module_check.BR;
import com.invengo.module_check.R;
import com.trello.rxlifecycle2.LifecycleProvider;

import me.goldze.mvvmhabit.bus.Messenger;
import me.goldze.mvvmhabit.utils.constant.Constants;

public class ChangeFirstDialog {
    public ChangeFistViewModel viewModel;
    private static ChangeFirstDialog instance;
    private ViewDataBinding binding;
    private MaterialDialog dialog;

    public static ChangeFirstDialog getInstance() {
        if (instance == null) synchronized (ChangeFirstDialog.class) {
            if (instance == null) {
                instance = new ChangeFirstDialog();
            }
        }
        return instance;
    }

    private ChangeFirstDialog() {
    }

    public void showDialog(FragmentActivity activity, ShelfBean bean) {
        if (dialog == null) {
            initDialog(activity);
        }
        if (!dialog.isShowing()) {
            dialog.show();
        }
        viewModel.updateDialogBook(bean);
    }

    private void initDialog(FragmentActivity activity) {
        viewModel = ViewModelProviders.of(activity).get(ChangeFistViewModel.class);
        binding = DataBindingUtil.inflate(LayoutInflater.from(activity),
                R.layout.dialog_change_first, null, false);
        if (dialog == null)
            dialog = new MaterialDialog.Builder(activity)
                    .customView(binding.getRoot(), false)
                    .showListener(dialog -> {
                        DeviceHelper.getInstance().addTempCache("这是一个奇怪的界面key");
                        viewModel.registerEvent("这是一个奇怪的界面key");
                    })
                    .dismissListener(dialog -> {
                        Messenger.getDefault().send("message",Constants.TOKEN_REGISTER_AGAIN);
                        viewModel.isReading.set(false);
                        viewModel.removeEvent();
                    })
                    .build();
        dialog.setOnKeyListener((dialog, keyCode, event) -> {
            if (keyCode == 300) {
                return activity.onKeyDown(keyCode, event);
            }
            return false;
        });
        binding.setVariable(BR.viewModel, viewModel);
        binding.setLifecycleOwner(activity);
        //让ViewModel拥有View的生命周期感应
        activity.getLifecycle().addObserver(viewModel);
        //注入RxLifecycle生命周期
        viewModel.injectLifecycleProvider((LifecycleProvider) activity);
        viewModel.ui.changeBookEvent.observe(binding.getLifecycleOwner(), bean -> {
            Messenger.getDefault().send(bean, Constants.TOKEN_DIALOG_2_FIRST_CHECK);
            dialog.dismiss();
        });
    }
}
